﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
namespace AgriMarketService
{
    public class FileProvider
    {
        public static bool Upload(byte[] content, string fileName)
        {
            //
            try
            {
                MemoryStream memoryStream = new MemoryStream(content);
                string mapPath = System.Web.Hosting.HostingEnvironment.MapPath("~/");
                string filePath = mapPath + fileName;
                FileStream targetStream = new FileStream(filePath, FileMode.Create);
                memoryStream.WriteTo(targetStream);
                memoryStream.Close();
                targetStream.Close();
                targetStream.Dispose();
                return true;
            }
            catch(Exception e)
            {
                return false;
            }
        }
        public static byte[] Download(string fileName)
        {
            try
            {
                string mapPath = System.Web.Hosting.HostingEnvironment.MapPath("~/");
                string filePath = mapPath + fileName;
                FileInfo fileInfo = new FileInfo(filePath);

                FileStream fileStream = new FileStream(filePath,FileMode.Open,FileAccess.Read);

                BinaryReader binaryReader = new BinaryReader(fileStream);
                byte[] data = binaryReader.ReadBytes((int)fileInfo.Length);
                binaryReader.Close();
                return data;

            }
            catch
            {
                return null;
            }
        }
        public static bool Delete(string fileName)
        {
            try
            {
                string mapPath = System.Web.Hosting.HostingEnvironment.MapPath("~/");
                string filePath = mapPath + fileName;
                File.Delete(filePath);
                return true;
            }
            catch
            {
                return false;
            }
            
        }
    }
}